home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12184 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: news.spies.com!usenet
  2. From: Erik Max Francis <max@alcyone.com>
  3. Newsgroups: comp.lang.c,comp.unix.programmer,comp.unix.questions
  4. Subject: Re: Correct usage of system call?
  5. Date: Thu, 28 Mar 1996 19:56:16 -0800
  6. Organization: Alcyone Systems
  7. Message-ID: <315B5F60.6CEE6525@alcyone.com>
  8. References: <4jb03v$oi6@news.tamu.edu>
  9. NNTP-Posting-Host: newton.alcyone.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.01 (X11; I; Linux 1.2.13 i486)
  14.  
  15. Justin Ray Mcelhanon wrote:
  16.  
  17. > It works great, however, I'd like to add in a command to allow one to
  18. > type in the site name.  Example, when you call up the ftp case it does
  19. > a scanf(%s,site); (with site declared as an char string) anyway I then
  20. > want to take that and put it in the system command. ie system("ftp site"),
  21. > but that is not the proper syntax, how could I accomplish this?
  22.  
  23. Only the printf family takes the formatted arguments that you're trying to use
  24. (that's what the trailing `f' is for).  What you need to do is build a string
  25. for the system call and then execute system with that built string.
  26.  
  27. You can do this in two easy ways.  Assume that you have an array of chars s
  28. which contains enough space to hold your string (you have to ensure this
  29. yourself), and site is the user input string that has been properly read in.
  30. You can then
  31.  
  32.     strcpy(s, "ftp ");
  33.     strcat(s, site);
  34.     system(s);
  35.  
  36. or if you're lazy
  37.  
  38.     sprintf(s, "ftp %s", site);
  39.     system(s);
  40.  
  41. Note that s has to be a different array of char than site, or you will have
  42. problems.  If you're not familiar with what calls such as strcpy, strcat, and
  43. sprintf do, then check your reference manuals.
  44.  
  45. This is part of the natural learning curve involved in using C:  You come to
  46. realize that you are given the power (and responsibility!) of doing whatever
  47. you like.
  48.  
  49. -- 
  50. Erik Max Francis &tSftDotIotE && http://www.alcyone.com/max && max@alcyone.com
  51. San Jose, California, U.S.A. && 37 20 07 N 121 53 38 W && the 4th R is respect
  52. H.3`S,3,P,3$S,#$Q,C`Q,3,P,3$S,#$Q,3`Q,3,P,C$Q,#(Q.#`-"C`- && 1love && folasade
  53. Omnia quia sunt, lumina sunt. && Dominion, GIGO, GOOGOL, Omega, Psi, Strategem
  54. "Out from his breast/his soul went to seek/the doom of the just." -- _Beowulf_
  55.